{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Executing a Workflow (Lattice) Multiple Times\n",
    "\n",
    "Execute a lattice multiple times with different parameters by starting the lattice from a loop. Covalent parallelizes the execution of the runs. \n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "[Start the Covalent server](./covalent_cli.rst).\n",
    "\n",
    "### Procedure\n",
    "\n",
    "1. Write the lattice. This example takes a single scalar parameter, but the principle is the same for more complex inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "@ct.electron\n",
    "def identity(x):\n",
    "    return x\n",
    "\n",
    "@ct.electron\n",
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(a):\n",
    "    val_1 = identity(x=a)\n",
    "    return square(x=val_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Dispatch the tasks separately:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['08a5f022-0bb7-4f70-b7bd-6a6806931e40',\n",
       " 'd4a3e660-63a2-47b0-8d3e-722b697bcd7d',\n",
       " 'e34ed7ef-be70-4700-954b-9ed7330b2a86',\n",
       " '1cffea94-5d82-4186-a352-0a510610e4cc']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "params = [1, 2, 3, 4]\n",
    "dispatch_ids = [ct.dispatch(workflow)(a=param) for param in params]\n",
    "\n",
    "dispatch_ids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. The dispatch IDs are your keys to the results of each run; you will use them to process or view the results. One option is to simply save the dispatch IDs so that you can retrieve the results later:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"dispatch_ids.txt\", 'w') as id_file:\n",
    "    id_file.writelines(\"%s\\n\" % id for id in dispatch_ids)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Looping](../coding/looping.ipynb)\n",
    "\n",
    "\n",
    "[Executing a Lattice as an Electron (Sublattice)](./execute_sublattice.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
